/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.manage.service;

import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.manage.core.manage.convertor.RelationManageConvertor;
import info.peigen.hotpot.manage.core.manage.form.RelationCreateForm;
import info.peigen.hotpot.manage.core.manage.form.RelationEditForm;
import info.peigen.hotpot.manage.core.manage.form.RelationSearchForm;
import info.peigen.hotpot.manage.core.manage.repository.service.RelationManageRepository;
import info.peigen.hotpot.manage.core.repository.entity.RelationEntity;
import info.peigen.hotpot.manage.core.service.RelationService;
import info.peigen.hotpot.manage.core.service.order.RelationCreateOrder;
import info.peigen.hotpot.manage.core.service.order.RelationEditOrder;
import info.peigen.hotpot.manage.core.service.result.RelationCreateResult;
import info.peigen.hotpot.manage.core.service.result.RelationEditResult;
import info.peigen.hotpot.manage.core.service.result.info.RelationInfo;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.List;

/**
 * <b>(RelationManageService)</b>
 * 管理后台 "关联关系" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-08-05
 */
@SuppressWarnings("unused")
@Component
public class RelationManageService {
    @Inject
    RelationManageRepository relationManageRepository;
    @Inject
    RelationService          relationService;

    public PageResult<RelationInfo> page(RelationSearchForm form) {
        LambdaQuery<RelationEntity> lambdaQuery = query(form);
        return relationManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, RelationManageConvertor.INSTANCE::convertToInfo);
    }

    public List<RelationInfo> list(RelationSearchForm form) {
        LambdaQuery<RelationEntity> lambdaQuery = query(form);
        List<RelationEntity>        entities    = lambdaQuery.select();
        return entities.stream().map(RelationManageConvertor.INSTANCE::convertToInfo).toList();
    }

    public RelationInfo create(RelationCreateForm form) {
        RelationCreateOrder  order  = RelationManageConvertor.INSTANCE.convertToCreateOrder(form);
        RelationCreateResult result = relationService.create(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public RelationInfo edit(RelationEditForm form) {
        RelationEditOrder  order  = RelationManageConvertor.INSTANCE.convertToEditOrder(form);
        RelationEditResult result = relationService.edit(order);
        if (result.success()) {
            return result.getInfo();
        }
        return null;
    }

    public void remove(Integer id) {
        relationManageRepository.delete(id);
    }

    public void removes(List<Integer> ids) {
        relationManageRepository.removeByIds(ids);
    }

    public RelationInfo detail(Integer id) {
        RelationEntity entity = relationManageRepository.getMapper().single(id);
        return RelationManageConvertor.INSTANCE.convertToInfo(entity);
    }

    public RelationInfo detail(String relationId) {
        RelationEntity entity = relationManageRepository.getMapper().findByRelationId(relationId);
        return RelationManageConvertor.INSTANCE.convertToInfo(entity);
    }

    private LambdaQuery<RelationEntity> query(RelationSearchForm form) {
        return relationManageRepository.getMapper().createLambdaQuery()
                .andLike(RelationEntity::getRelationId, Query.filterLikeEmpty(form.getRelationId(), true, true))
                .andLike(RelationEntity::getObjectId, Query.filterLikeEmpty(form.getObjectId(), true, true))
                .andLike(RelationEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), true, true))
                .desc(form.getOrderName())
                ;
    }

}